package com.imooc.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.imooc.entity.BaseEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        if (metaObject.getOriginalObject() instanceof BaseEntity) {
            Long userId = 1L;
            this.strictInsertFill(metaObject, "createBy", Long.class, userId);
            Long createDeptId = 1L;
            this.strictInsertFill(metaObject, "createDeptId", Long.class, createDeptId);
            LocalDateTime createTime = LocalDateTime.now();
            this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, createTime);
            Long updateBy = 1L;
            this.strictInsertFill(metaObject, "updateBy", Long.class, updateBy);
            LocalDateTime updateTime = LocalDateTime.now();
            this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, updateTime);
            this.strictInsertFill(metaObject, "flagDel", Integer.class, 0);
            this.strictInsertFill(metaObject, "version", Integer.class, 0);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long createDeptId = 1L;
        this.strictUpdateFill(metaObject, "createDeptId", Long.class, createDeptId);
        Long updateBy = 1L;
        this.strictUpdateFill(metaObject, "updateBy", Long.class, updateBy);
        LocalDateTime updateTime = LocalDateTime.now();
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, updateTime);
        log.info("start update fill ....");
    }
}
